#include "listener.h"
using namespace std;
Listener::Listener(string dirn, string port)
{

   this->dirn=dirn;
   this->port=port;

}

int Listener::exec()
{
    while(1)
    {
        //QSqlQuery query(m_db);
        MySocket socket;
        socket.myBind(port.c_str());
        socket.myListen(5);

        while(true)
        {

            struct sockaddr* addr = (sockaddr*)malloc(sizeof(struct sockaddr_in6));
            socklen_t length = sizeof(struct sockaddr_in6);

            MySocket clientSocket = socket.myAccept(addr, &length);

            char filename[256];
            clientSocket.read(filename, 256*sizeof(char));
            Sender *sender = new Sender(clientSocket,filename,dirn);
            QThread *senderThread = new QThread(this);

            connect(senderThread, SIGNAL(started()), sender, SLOT(send()));
            sender->moveToThread(senderThread);

                // Starts an event loop, and emits workerThread->started()
            senderThread->start();

            free(addr);
        }

    }
    return 0;
}
